﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Threading;
using System.Diagnostics;

namespace Microsoft.Forums.ForumsClient.Models
{
    /// <summary>
    /// An abstract base class for any object that would like to be easily bound to UI elements
    /// </summary>
    public abstract class BindableObject : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// Create the object that is easily bound
        /// </summary>
        protected BindableObject()
        {
            
        }

        /// <summary>
        /// Fire the property changed event for the given property name
        /// </summary>
        /// <param name="propertyName">Name of the property</param>
        protected void OnPropertyChanged(string propertyName)
        {
            VerifyProperty(propertyName);
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Used in debug mode only.  Checks to make sure the property name string
        /// is actually a property on the object.
        /// 
        /// This idea came from Josh Smith's Podder podcast application
        /// </summary>
        /// <param name="propertyName">Name of the property</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)"), Conditional("DEBUG")]
        private void VerifyProperty(string propertyName)
        {
            bool propertyExists = TypeDescriptor.GetProperties(this).Find(propertyName, false) != null;
            if (!propertyExists)
            {
                Debug.Fail(String.Format("The property {0} could not be found in {1}",propertyName,GetType().FullName));
            }
        }


    }
}
